<?php
/*
 * main_gauss2rdata.php
 *
 */


 mb_internal_encoding("utf-8");
 //gc_enabled();
 require "../../document/class_corpus.php";
 require "../../document/class_document.php";
 require "../../document/class_document_xml.php";

 require "../../files/tool_files_dirs.php";

 require "../../misc/class_tag.php";
 require "../../misc/tool_color.php";

 require "../../tree/class_tree.php";
 require "../../tree/class_tree_xhtml.php";
 require "../../tree/class_structure.php";
 require "../../tree/tool_tree.php";

 require "../../array/tool_array.php";

 require "../../pear_graph/tool_pear_graph.php";

 require "../../maths/tool_maths.php";

 require "../../string/tool_string.php";
 require "../../string/class_segment.php";
 require "../../pdo/class_sql_document.php";

function fill_graph(&$graph,$data_x,$data_y){
  fill_graph_aux($graph,$data_x,$data_y);
  return $graph->plot4();
}

function fill_graph_aux(&$graph,$data_x,$data_y){
  $graph->__add_array_x($data_x);
  $graph->__add_array_y($data_y);
}

$exp=new sql_experience("align_ecr_word");
$array_obj_sql=$exp->reader_experience();
$array_data=array();

$data_lg=$array_obj_sql['sql_document']->reader_all_lg();

$data_lg=array(
  array('lg'=>"fr"),
//  array('lg'=>"es"),
//  array('lg'=>"hu"),
  array('lg'=>"cs")
);

foreach($data_lg as $ligne){
  $array_id_document=$array_obj_sql['sql_document']->reader_lg2array_id_document_id_bloc($ligne['lg']);
  $correspondance_doc_bloc=array();
  $correspondance_bloc_doc=array();
  foreach($array_id_document as $ligne_doc){
    $correspondance_doc_bloc[$ligne_doc['id_document']][]=$ligne_doc['id_bloc'];
    $correspondance_bloc_doc[$ligne_doc['id_bloc']]=$ligne_doc['id_document'];
  }
  $d=$array_obj_sql['sql_rstr']->reader_all2count_bloc($ligne['lg']);
  $repartition_bloc=$repartition_global_len=array();
  $repartition_global_freq=$repartition_doc_freq=array();
  foreach($d as $l){
    $id_doc=$correspondance_bloc_doc[$l['id_bloc']];
    $repartition_bloc[$l['graphie']][$l['id_bloc']]=$l['COUNT(r.id_bloc)'];
    $repartition_global_len[$l['len']]+=$l['COUNT(r.id_bloc)'];
    $repartition_doc_len[$id_doc][$l['len']]+=$l['COUNT(r.id_bloc)'];
    $repartition_global_freq[$l['graphie']]+=$l['COUNT(r.id_bloc)'];
    $repartition_doc_freq[$id_doc][$l['graphie']]+=$l['COUNT(r.id_bloc)'];
  }

  $zipf=tool_maths::zipf($repartition_global_freq);

  $array_data[$ligne['lg']]=array(
    $correspondance_bloc_doc,
    $correspondance_doc_bloc,
    $repartition_bloc,
    $repartition_global_len,
    $repartition_doc_len,
    $repartition_global_freq,
    $repartition_doc_freq
  );

}

$motif_lg="fr";

$data_motif=$array_data[$motif_lg];
//print_r($data_motif[6]);
//die();

function clean_rep($_rep){
  $new_rep = array(0=>0);
  for($i=1;;++$i){
    if(!isset($_rep[$i])){
      return $new_rep;
    }
    $new_rep[$i]=$_rep[$i];
  }
}

$tool_graph=new Tool_pear_graph();
$tool_graph->__set_width(500);
$tool_graph->__set_height(510);
$tool_graph->__add_title("Répartition des mots par leurs longueurs");

foreach($array_data as $motif_lg=>$data_motif){
  $rep_global_freq=$data_motif[5];
  arsort($rep_global_freq);
  $zipf=tool_maths::own_zipf($rep_global_freq);
  $clean_rep = clean_rep($zipf[4]);
  fill_graph_aux($tool_graph,array_keys($clean_rep),array_values($clean_rep));
  $tool_graph->__add_plot_title("$motif_lg");
  $keys=array_keys($clean_rep);
  $min=min($keys); $max=max($keys);
  $array_k=array();
  for($min;$min<$max;++$min) $array_k[]=$min;
  $gauss=tool_maths::get_gaussienne($clean_rep,$array_k);
  $gauss["repr"] = clean_rep($gauss["repr"]);

  fill_graph_aux($tool_graph,array_keys($gauss["repr"]),array_values($gauss['repr']));
  $tool_graph->__add_plot_title("gaussienne $motif_lg");

//  print_r($gauss_repr);
}

$graph=$tool_graph->line01();
ob_start();
  $graph->done();
  $graph_svg=ob_get_contents();
ob_clean();
Tool_files::file_write("./4_gauss.svg",$graph_svg);

die();
